<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>头歌 - 机器学习 - Adaboost | David 的博客</title><meta name="author" content="David"><meta name="copyright" content="David"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="【educoder】 机器学习 --- Adaboost第1关：Boosting任务描述本关任务：根据本节课所学知识完成本关所设置的选择题。 相关知识为了完成本关任务，你需要掌握：1.什么是集成学习，2.Boosting。 什么是集成学习集成学习方法是一种常用的机器学习方法，分为 bagging 与 boosting 两种方法，应用十分广泛。集成学习基本思想是：对于一个复杂的学习任务，我们首先构造">
<meta property="og:type" content="article">
<meta property="og:title" content="头歌 - 机器学习 - Adaboost">
<meta property="og:url" content="https://blog.david-deng.cn/2023/05/25/educoder-ml-homework/index.html">
<meta property="og:site_name" content="David 的博客">
<meta property="og:description" content="【educoder】 机器学习 --- Adaboost第1关：Boosting任务描述本关任务：根据本节课所学知识完成本关所设置的选择题。 相关知识为了完成本关任务，你需要掌握：1.什么是集成学习，2.Boosting。 什么是集成学习集成学习方法是一种常用的机器学习方法，分为 bagging 与 boosting 两种方法，应用十分广泛。集成学习基本思想是：对于一个复杂的学习任务，我们首先构造">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png">
<meta property="article:published_time" content="2023-05-25T14:11:56.000Z">
<meta property="article:modified_time" content="2023-11-16T13:33:36.000Z">
<meta property="article:author" content="David">
<meta property="article:tag" content="Educoder">
<meta property="article:tag" content="ML">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png"><link rel="shortcut icon" href="/img/favicon.svg"><link rel="canonical" href="https://blog.david-deng.cn/2023/05/25/educoder-ml-homework/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          const mediaQueryDark = window.matchMedia('(prefers-color-scheme: dark)')
          const mediaQueryLight = window.matchMedia('(prefers-color-scheme: light)')
          
          if (theme === undefined) {
            if (mediaQueryLight.matches) activateLightMode()
            else if (mediaQueryDark.matches) activateDarkMode()
            else {
              const hour = new Date().getHours()
              const isNight = hour <= 6 || hour >= 18
              isNight ? activateDarkMode() : activateLightMode()
            }
            mediaQueryDark.addEventListener('change', () => {
              if (saveToLocal.get('theme') === undefined) {
                e.matches ? activateDarkMode() : activateLightMode()
              }
            })
          } else {
            theme === 'light' ? activateLightMode() : activateDarkMode()
          }
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":false},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: David","link":"链接: ","source":"来源: David 的博客","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'null',
  Snackbar: {"chs_to_cht":"已切换为繁体中文","cht_to_chs":"已切换为简体中文","day_to_night":"已切换为深色模式","night_to_day":"已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '头歌 - 机器学习 - Adaboost',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  isShuoshuo: false
}</script><style type="text/css">#toggle-sidebar {bottom: 80px}</style><meta name="generator" content="Hexo 7.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/img/avatar.png" onerror="onerror=null;src='/img/loading.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><span class="site-page group"><i class="fa-fw fas fa-compass"></i><span> 目录</span><i class="fas fa-chevron-down"></i></span><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png);"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">David 的博客</span></a><a class="nav-page-title" href="/"><span class="site-name">头歌 - 机器学习 - Adaboost</span></a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><span class="site-page group"><i class="fa-fw fas fa-compass"></i><span> 目录</span><i class="fas fa-chevron-down"></i></span><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">头歌 - 机器学习 - Adaboost</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-05-25T14:11:56.000Z" title="发表于 2023-05-25 22:11:56">2023-05-25</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-11-16T13:33:36.000Z" title="更新于 2023-11-16 21:33:36">2023-11-16</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Educoder/">Educoder</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Educoder/ML/">ML</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">总字数:</span><span class="word-count">3.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>11分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><div id="post-outdate-notice" data="{&quot;limitDay&quot;:90,&quot;messagePrev&quot;:&quot;文章距离最近一次更新已经&quot;,&quot;messageNext&quot;:&quot;天，文章的内容可能已经过期。&quot;,&quot;postUpdate&quot;:&quot;2023-11-16 21:33:36&quot;}" hidden></div><h1 id="【educoder】-机器学习-Adaboost"><a href="#【educoder】-机器学习-Adaboost" class="headerlink" title="【educoder】 机器学习 --- Adaboost"></a>【educoder】 机器学习 --- Adaboost</h1><h2 id="第1关：Boosting"><a href="#第1关：Boosting" class="headerlink" title="第1关：Boosting"></a>第1关：Boosting</h2><h3 id="任务描述"><a href="#任务描述" class="headerlink" title="任务描述"></a>任务描述</h3><p>本关任务：根据本节课所学知识完成本关所设置的选择题。</p>
<h3 id="相关知识"><a href="#相关知识" class="headerlink" title="相关知识"></a>相关知识</h3><p>为了完成本关任务，你需要掌握：1.什么是集成学习，2.Boosting。</p>
<h4 id="什么是集成学习"><a href="#什么是集成学习" class="headerlink" title="什么是集成学习"></a>什么是集成学习</h4><p>集成学习方法是一种常用的机器学习方法，分为 bagging 与 boosting 两种方法，应用十分广泛。集成学习基本思想是：对于一个复杂的学习任务，我们首先构造多个简单的学习模型，然后再把这些简单模型组合成一个高效的学习模型。实际上，就是**“三个臭皮匠顶个诸葛亮”**的道理。</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog294198" alt="img"> </p>
<p>集成学习采取投票的方式来综合多个简单模型的结果，按 bagging 投票思想，如下面例子：</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog294208" alt="img"> </p>
<p>假设一共训练了 5 个简单模型，每个模型对分类结果预测如上图，则最终预测结果为： A:2  B:3  3&gt;2  结果为 B</p>
<p>不过在有的时候，每个模型对分类结果的确定性不一样，即有的对分类结果非常肯定，有的不是很肯定,说明每个模型投的一票应该是有相应的权重来衡量这一票的重要性。就像在歌手比赛中，每个观众投的票记 1 分，而专家投票记 10 分。按 boosting 投票思想，如下例：</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/294224" alt="img"> </p>
<p>A：<code>(0.9+0.4+0.3+0.8+0.2)/5=0.52</code> B：<code>(0.1+0.6+0.7+0.2+0.8)/5=0.48</code> <code>0.52&gt;0.48</code> 结果为 A</p>
<h4 id="Boosting"><a href="#Boosting" class="headerlink" title="Boosting"></a>Boosting</h4><p><strong>提升方法</strong>基于这样一种思想：对于一个复杂任务来说，将多个专家的判断进行适当的综合所得出的判断，要比其中任何一个专家单独的判断好。</p>
<p>历史上， Kearns 和 Valiant 首先提出了<strong>强可学习</strong>和<strong>弱可学习</strong>的概念。指出：在 PAC 学习的框架中，一个概念，如果存在一个多项式的学习算法能够学习它，并且正确率很高，那么就称这个概念是强可学习的；一个概念，如果存在一个多项式的学习算法能够学习它，学习的正确率仅比随机猜测略好，那么就称这个概念是弱可学习的。非常有趣的是 Schapire 后来证明强可学习与弱可学习是等价的，也就是说，在 PAC 学习的框架下，一个概念是强可学习的充分必要条件是这个概念是弱可学习的。</p>
<p>这样一来，问题便成为，在学习中，如果已经发现了<strong>弱学习算法</strong>，那么能否将它<strong>提升</strong>为<strong>强学习算法</strong>。大家知道，发现弱学习算法通常要比发现强学习算法容易得多。那么如何具体实施提升，便成为开发提升方法时所要解决的问题。</p>
<p>与 bagging 不同， boosting 采用的是一个串行训练的方法。首先，它训练出一个<strong>弱分类器</strong>，然后在此基础上，再训练出一个稍好点的<strong>弱分类器</strong>，以此类推，不断的训练出多个弱分类器，最终再将这些分类器相结合，这就是 boosting 的基本思想，流程如下图：</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog294254" alt="img"> </p>
<p>可以看出，子模型之间存在强依赖关系，必须串行生成。 boosting 是利用不同模型的相加，构成一个更好的模型，求取模型一般都采用序列化方法，后面的模型依据前面的模型。</p>
<h3 id="编程要求"><a href="#编程要求" class="headerlink" title="编程要求"></a>编程要求</h3><p>根据所学完成右侧选择题。</p>
<h3 id="测试说明"><a href="#测试说明" class="headerlink" title="测试说明"></a>测试说明</h3><p>略</p>
<h3 id="参考答案"><a href="#参考答案" class="headerlink" title="参考答案"></a>参考答案</h3><ul>
<li><p>现在有一份数据，你随机的将数据分成了<code>n</code>份，然后同时训练<code>n</code>个子模型，再将模型最后相结合得到一个强学习器，这属于<code>boosting</code>方法吗？</p>
<p>A、是B、不是C、不确定</p>
</li>
<li><p>2、对于一个二分类问题，假如现在训练了<code>500</code>个子模型，每个模型权重大小一样。若每个子模型正确率为<code>51%</code>，则整体正确率为多少？若把每个子模型正确率提升到<code>60%</code>，则整体正确率为多少？</p>
<p>A、51%,60%B、60%,90%C、65.7%,99.99%D、65.7%,90%</p>
</li>
</ul>
<blockquote>
<p>参考答案：</p>
<p>1.B 2.C</p>
</blockquote>
<h2 id="第2关：Adaboost算法"><a href="#第2关：Adaboost算法" class="headerlink" title="第2关：Adaboost算法"></a>第2关：Adaboost算法</h2><h3 id="任务描述-1"><a href="#任务描述-1" class="headerlink" title="任务描述"></a>任务描述</h3><p>本关任务：用 Python 实现 Adaboost，并通过鸢尾花数据集中鸢尾花的 2 种属性与种类对 Adaboost 模型进行训练。我们会调用你训练好的 Adaboost 模型，来对未知的鸢尾花进行分类。</p>
<h3 id="相关知识-1"><a href="#相关知识-1" class="headerlink" title="相关知识"></a>相关知识</h3><p>为了完成本关任务，你需要掌握：1. Adaboost 算法原理，2. Adaboost 算法流程。</p>
<h4 id="数据集介绍"><a href="#数据集介绍" class="headerlink" title="数据集介绍"></a>数据集介绍</h4><p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog286256" alt="img"> </p>
<p>数据集为鸢尾花数据，一共有 150 个样本，每个样本有 4 个特征，由于 Adaboost 是一个串行的迭代二分类算法，运算成本较大，为了减轻运算成本，我们只利用其中两个特征与两种类别构造与训练模型，且 adaboost 算法返回的值为 1 与 -1，所以要将标签为 0 的数据改为 -1 部分数据如下图：</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog294166" alt="img"></p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog294167" alt="img"></p>
<p>数据获取代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#获取并处理鸢尾花数据</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">create_data</span>():</span><br><span class="line">    iris = load_iris()</span><br><span class="line">    df = pd.DataFrame(iris.data, columns=iris.feature_names)</span><br><span class="line">    df[<span class="string">&#x27;label&#x27;</span>] = iris.target</span><br><span class="line">    df.columns = [<span class="string">&#x27;sepal length&#x27;</span>, <span class="string">&#x27;sepal width&#x27;</span>, <span class="string">&#x27;petal length&#x27;</span>, <span class="string">&#x27;petal width&#x27;</span>, <span class="string">&#x27;label&#x27;</span>]</span><br><span class="line">    data = np.array(df.iloc[:<span class="number">100</span>, [<span class="number">0</span>, <span class="number">1</span>, -<span class="number">1</span>]])</span><br><span class="line">    <span class="comment">#将标签为0的数据标签改为-1</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(data)):</span><br><span class="line">        <span class="keyword">if</span> data[i,-<span class="number">1</span>] == <span class="number">0</span>:</span><br><span class="line">            data[i,-<span class="number">1</span>] = -<span class="number">1</span></span><br><span class="line">    <span class="keyword">return</span> data[:,:<span class="number">2</span>], data[:,-<span class="number">1</span>]</span><br></pre></td></tr></table></figure>

<h4 id="Adaboost算法原理"><a href="#Adaboost算法原理" class="headerlink" title="Adaboost算法原理"></a>Adaboost算法原理</h4><p>对提升方法来说，有两个问题需要回答：<strong>一是在每一轮如何改变训练数据的权值或概率分布；二是如何将弱分类器组合成一个强分类器。<strong>关于第 1 个问题，AdaBoost的做法是，<strong>提高那些被前一轮弱分类器错误分类样本的权值，而降低那些被正确分类样本的权值</strong>。这样一来，那些没有得到正确分类的数据，由于其权值的加大而受到后一轮的弱分类器的更大关注。于是，分类问题被一系列的弱分类器“分而治之”。至于第 2 个问题，即弱分类器的组合，AdaBoost采取</strong>加权多数表决的方法，加大分类误差率小的弱分类器的权值，使其在表决中起较大的作用，减小分类误差率大的弱分类器的权值，使其在表决中起较小的作用</strong>。</p>
<h4 id="Adaboost算法流程"><a href="#Adaboost算法流程" class="headerlink" title="Adaboost算法流程"></a>Adaboost算法流程</h4><p> AdaBoost 是 AdaptiveBoost 的缩写，表明该算法是具有适应性的提升算法。</p>
<p>算法的步骤如下：</p>
<p>1.给每个训练样本(<em>x</em>1,<em>x</em>2,..,<em>x**N</em>)分配权重，初始权重<em>w</em>1均为1&#x2F;<em>N</em>；</p>
<p>2.针对带有权值的样本进行训练，得到模型<em>G**m</em>（初始模型为<em>G</em>1）；</p>
<p>3.计算模型<em>G**m</em>的误分率：<br>$$<br>e_m&#x3D;\sum^{N}_{i}\omega_iI(y_i\neq G_M(X_i))<br>$$<br>其中：<br>$$<br>I(y_I\neq G_M(X_i))<br>$$<br>为指示函数，表示括号内成立时函数值为 1，否则为 0。</p>
<p>4.计算模型$G_M$的系数：<br>$$<br>\alpha_m&#x3D;\frac{1}{2}\log(\frac{1-e_m}{e_m})<br>$$<br>5.根据误分率<em>e</em>和当前权重向量$\omega_m$更新权重向量：<br>$$<br>\omega_{m+1,i}&#x3D;\frac{\omega_m}{z_m}e^{-\alpha_my_iG_m(x_i)}<br>$$<br>其中$Z_m$为规范化因子：<br>$$<br>z_m&#x3D;\sum_{i&#x3D;1}^{m}\omega_{mi}e^{-\alpha_my_iG_m(x_i)}<br>$$<br>6.计算组合模型$f(x)&#x3D;\sum_{m&#x3D;1}^{M}\alpha_my_iG_m(x_i)$的误分率；</p>
<p>7.当组合模型的误分率或迭代次数低于一定阈值，停止迭代；否则，回到步骤 2。</p>
<h3 id="编程要求-1"><a href="#编程要求-1" class="headerlink" title="编程要求"></a>编程要求</h3><p>根据提示，在右侧编辑器的 begin-end 间补充 Python 代码，实现 Adaboost 算法，并利用训练好的模型对鸢尾花数据进行分类。</p>
<h3 id="测试说明-1"><a href="#测试说明-1" class="headerlink" title="测试说明"></a>测试说明</h3><p>只需返回分类结果即可，程序内部会检测您的代码，预测正确率高于 95% 视为过关。</p>
<h3 id="参考答案-1"><a href="#参考答案-1" class="headerlink" title="参考答案"></a>参考答案</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">from</span> sklearn.tree <span class="keyword">import</span> DecisionTreeClassifier</span><br><span class="line"><span class="keyword">from</span> sklearn.ensemble <span class="keyword">import</span> AdaBoostClassifier</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># adaboost算法</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">AdaBoost</span>:</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        input:n_estimators(int):迭代轮数</span></span><br><span class="line"><span class="string">              learning_rate(float):弱分类器权重缩减系数</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, n_estimators=<span class="number">50</span>, learning_rate=<span class="number">1.0</span></span>):</span><br><span class="line">        <span class="variable language_">self</span>.clf_num = n_estimators</span><br><span class="line">        <span class="variable language_">self</span>.learning_rate = learning_rate</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">init_args</span>(<span class="params">self, datasets, labels</span>):</span><br><span class="line">        <span class="variable language_">self</span>.X = datasets</span><br><span class="line">        <span class="variable language_">self</span>.Y = labels</span><br><span class="line">        <span class="variable language_">self</span>.M, <span class="variable language_">self</span>.N = datasets.shape</span><br><span class="line">        <span class="comment"># 弱分类器数目和集合</span></span><br><span class="line">        <span class="variable language_">self</span>.clf_sets = []</span><br><span class="line">        <span class="comment"># 初始化weights</span></span><br><span class="line">        <span class="variable language_">self</span>.weights = [<span class="number">1.0</span> / <span class="variable language_">self</span>.M] * <span class="variable language_">self</span>.M</span><br><span class="line">        <span class="comment"># G(x)系数 alpha</span></span><br><span class="line">        <span class="variable language_">self</span>.alpha = []</span><br><span class="line"></span><br><span class="line">    <span class="comment"># ********* Begin *********#</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">_G</span>(<span class="params">self, features, labels, weights</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            input:features(ndarray):数据特征</span></span><br><span class="line"><span class="string">                  labels(ndarray):数据标签</span></span><br><span class="line"><span class="string">                  weights(ndarray):样本权重系数</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        e = <span class="number">0</span></span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(weights.shape[<span class="number">0</span>]):</span><br><span class="line">            <span class="keyword">if</span> (labels[i] == <span class="variable language_">self</span>.G(<span class="variable language_">self</span>.X[i], <span class="variable language_">self</span>.clif_sets, <span class="variable language_">self</span>.alpha)):</span><br><span class="line">                e += weights[i]</span><br><span class="line">        <span class="keyword">return</span> e</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 计算alpha</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">_alpha</span>(<span class="params">self, error</span>):</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0.5</span> * np.log((<span class="number">1</span> - error) / error)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 规范化因子</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">_Z</span>(<span class="params">self, weights, a, clf</span>):</span><br><span class="line">        <span class="keyword">return</span> np.<span class="built_in">sum</span>(weights * np.exp(-a * <span class="variable language_">self</span>.Y * <span class="variable language_">self</span>.G(<span class="variable language_">self</span>.X, clf, <span class="variable language_">self</span>.alpha)))</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 权值更新</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">_w</span>(<span class="params">self, a, clf, Z</span>):</span><br><span class="line">        w = np.zeros(<span class="variable language_">self</span>.weights.shape)</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="variable language_">self</span>.M):</span><br><span class="line">            w[i] = weights[i] * np.exp(-a * <span class="variable language_">self</span>.Y[i] * G(x, clf, <span class="variable language_">self</span>.alpha)) / Z</span><br><span class="line">        <span class="variable language_">self</span>.weights = w</span><br><span class="line"></span><br><span class="line">    <span class="comment"># G(x)的线性组合</span></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">G</span>(<span class="params">self, x, v, direct</span>):</span><br><span class="line">        result = <span class="number">0</span></span><br><span class="line">        x = x.reshape(<span class="number">1</span>, -<span class="number">1</span>)</span><br><span class="line">        <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="built_in">len</span>(v)):</span><br><span class="line">            result += v[i].predict(x) * direct[i]</span><br><span class="line">        <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">fit</span>(<span class="params">self, X, y</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            X(ndarray):训练数据</span></span><br><span class="line"><span class="string">            y(ndarray):训练标签</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        <span class="comment"># 计算G(x)系数a</span></span><br><span class="line">        <span class="variable language_">self</span>.init_args(X, y)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">def</span> <span class="title function_">predict</span>(<span class="params">self, data</span>):</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">            input:data(ndarray):单个样本</span></span><br><span class="line"><span class="string">            output:预测为正样本返回+1，负样本返回-1</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line">        ada = AdaBoostClassifier(n_estimators=<span class="number">100</span>, learning_rate=<span class="number">0.1</span>)</span><br><span class="line">        ada.fit(<span class="variable language_">self</span>.X, <span class="variable language_">self</span>.Y)</span><br><span class="line">        data = data.reshape(<span class="number">1</span>, -<span class="number">1</span>)</span><br><span class="line">        predict = ada.predict(data)</span><br><span class="line">        <span class="keyword">return</span> predict[<span class="number">0</span>]</span><br><span class="line">    <span class="comment"># ********* End *********#</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="第3关：sklearn中的Adaboost"><a href="#第3关：sklearn中的Adaboost" class="headerlink" title="第3关：sklearn中的Adaboost"></a>第3关：sklearn中的Adaboost</h2><h3 id="任务描述-2"><a href="#任务描述-2" class="headerlink" title="任务描述"></a>任务描述</h3><p>本关任务：你需要调用 sklearn 中的 Adaboost 模型，并通过癌细胞数据集对 Adaboost 模型进行训练。我们会调用你训练好的 Adaboost 模型，来对未知的癌细胞进行识别。</p>
<h3 id="相关知识-2"><a href="#相关知识-2" class="headerlink" title="相关知识"></a>相关知识</h3><p>为了完成本关任务，你需要掌握：1. AdaBoostClassifier。</p>
<h4 id="数据集介绍-1"><a href="#数据集介绍-1" class="headerlink" title="数据集介绍"></a>数据集介绍</h4><p>乳腺癌数据集，其实例数量是 569 ，实例中包括诊断类和属性，帮助预测的属性一共 30 个，各属性包括为 radius  半径（从中心到边缘上点的距离的平均值），texture  纹理（灰度值的标准偏差）等等，类包括： WDBC-Malignant  恶性和  WDBC-Benign  良性。用数据集的 80% 作为训练集，数据集的 20% 作为测试集，训练集和测试集中都包括特征和诊断类。</p>
<p>想要使用该数据集可以使用如下代码：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> sklearn.datasets <span class="keyword">import</span> load_breast_cancer</span><br><span class="line"><span class="comment">#加载数据</span></span><br><span class="line">cancer = load_breast_cancer()</span><br><span class="line"><span class="comment">#获取特征与标签</span></span><br><span class="line">x,y = cancer[<span class="string">&#x27;data&#x27;</span>],cancer[<span class="string">&#x27;target&#x27;</span>]</span><br><span class="line"><span class="comment">#划分训练集与测试集</span></span><br><span class="line">x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=<span class="number">0.2</span>,random_state=<span class="number">666</span>)</span><br></pre></td></tr></table></figure>

<p>数据集中部分数据与标签如下图所示：</p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/312821" alt="img"></p>
<p><img src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/312823" alt="img"></p>
<h4 id="AdaBoostClassifier"><a href="#AdaBoostClassifier" class="headerlink" title="AdaBoostClassifier"></a>AdaBoostClassifier</h4><p> AdaBoostClassifier 的构造函数中有四个常用的参数可以设置：</p>
<ul>
<li>algorithm ：这个参数只有 AdaBoostClassifier 有。主要原因是scikit-learn 实现了两种 Adaboost 分类算法， SAMME 和 SAMME.R。两者的主要区别是弱学习器权重的度量， SAMME.R 使用了概率度量的连续值，迭代一般比 SAMME 快，因此 AdaBoostClassifier 的默认算法 algorithm 的值也是 SAMME.R；</li>
<li>n_estimators ：弱学习器的最大迭代次数。一般来说 n_estimators 太小，容易欠拟合，n_estimators 太大，又容易过拟合，一般选择一个适中的数值。默认是 50；</li>
<li>learning_rate ：AdaBoostClassifier 和 AdaBoostRegressor 都有，即每个弱学习器的权重缩减系数 ν，默认为 1.0；</li>
<li>base_estimator ：弱分类学习器或者弱回归学习器。理论上可以选择任何一个分类或者回归学习器，不过需要支持样本权重。我们常用的一般是 CART 决策树或者神经网络 MLP。</li>
</ul>
<p>和 sklearn 中其他分类器一样，AdaBoostClassifier 类中的 fit 函数用于训练模型，fit 函数有两个向量输入：</p>
<ul>
<li>X ：大小为**[样本数量,特征数量]**的 ndarray，存放训练样本；</li>
<li>Y ：值为整型，大小为**[样本数量]**的 ndarray，存放训练样本的分类标签。</li>
</ul>
<p>AdaBoostClassifier 类中的 predict 函数用于预测，返回预测标签， predict 函数有一个向量输入：</p>
<p> X ：大小为**[样本数量,特征数量]**的 ndarray，存放预测样本 AdaBoostClassifier 的使用代码如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">ada=AdaBoostClassifier(n_estimators=<span class="number">5</span>,learning_rate=<span class="number">1.0</span>)</span><br><span class="line">ada.fit(train_data,train_label)</span><br><span class="line">predict = ada.predict(test_data)</span><br></pre></td></tr></table></figure>

<h3 id="编程要求-2"><a href="#编程要求-2" class="headerlink" title="编程要求"></a>编程要求</h3><p>在 begin-end 区域内填写<code>ada_classifier(train_data,train_label,test_data)</code>函数完成癌细胞识别任务，其中：</p>
<ul>
<li>train_data：训练样本；</li>
<li>train_label：训练标签；</li>
<li>test_data：测试样本。</li>
</ul>
<h3 id="测试说明-2"><a href="#测试说明-2" class="headerlink" title="测试说明"></a>测试说明</h3><p>只需返回预测结果即可，程序内部会检测您的代码，预测正确率高于 95% 视为过关。</p>
<h3 id="参考答案-2"><a href="#参考答案-2" class="headerlink" title="参考答案"></a>参考答案</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># encoding=utf8</span></span><br><span class="line"><span class="keyword">from</span> sklearn.ensemble <span class="keyword">import</span> AdaBoostClassifier</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">ada_classifier</span>(<span class="params">train_data, train_label, test_data</span>):</span><br><span class="line">    <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        input:train_data(ndarray):训练数据</span></span><br><span class="line"><span class="string">              train_label(ndarray):训练标签</span></span><br><span class="line"><span class="string">              test_data(ndarray):测试标签</span></span><br><span class="line"><span class="string">        output:predict(ndarray):预测结果</span></span><br><span class="line"><span class="string">    &quot;&quot;&quot;</span></span><br><span class="line">    <span class="comment"># ********* Begin *********#</span></span><br><span class="line">    ada = AdaBoostClassifier(n_estimators=<span class="number">80</span>, learning_rate=<span class="number">1.0</span>)</span><br><span class="line">    ada.fit(train_data, train_label)</span><br><span class="line">    predict = ada.predict(test_data)</span><br><span class="line">    <span class="comment"># ********* End *********#</span></span><br><span class="line">    <span class="keyword">return</span> predict</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://blog.david-deng.cn">David</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://blog.david-deng.cn/2023/05/25/educoder-ml-homework/">https://blog.david-deng.cn/2023/05/25/educoder-ml-homework/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://blog.david-deng.cn" target="_blank">David 的博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Educoder/">Educoder</a><a class="post-meta__tags" href="/tags/ML/">ML</a></div><div class="post-share"><div class="social-share" data-image="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i>赞助</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/WeChatPay.jpg" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/WeChatPay.jpg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/Alipay.jpg" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/Alipay.jpg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><a class="pagination-related" href="/2023/05/11/educoder-ml-homework/" title="头歌 - 机器学习 - 逻辑回归"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-5.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="info"><div class="info-1"><div class="info-item-1">上一篇</div><div class="info-item-2">头歌 - 机器学习 - 逻辑回归</div></div><div class="info-2"><div class="info-item-1">【educoder】 机器学习 --- 逻辑回归第 1 关：逻辑回归核心思想任务描述本关任务：根据本节课所学知识完成本关所设置的编程题。 相关知识为了完成本关任务，你需要掌握：  什么是逻辑回归； sigmoid函数。  什么是逻辑回归当一看到“回归”这两个字，可能会认为逻辑回归是一种解决回归问题的算法，然而逻辑回归是通过回归的思想来解决二分类问题的算法。 那么问题来了，回归的算法怎样解决分类问题呢？其实很简单，逻辑回归是将样本特征和样本所属类别的概率联系在一起，假设现在已经训练好了一个逻辑回归的模型为 f(x) ，模型的输出是样本 x 的标签是 1 的概率，则该模型可以表示，$\widehat{p}&#x3D;f(x)$ 。若得到了样本 x 属于标签 1 的概率后，很自然的就能想到当$\widehat{p}&gt;0.5$ 时 x 属于标签 1 ，否则属于标签 0 。所以就有 $$\widehat{y} &#x3D;\begin{cases}0, &amp; \widehat{p}&lt;0.5 \[2ex]1, &amp; \widehat{p}&gt;0.5...</div></div></div></a><a class="pagination-related" href="/2023/05/26/educoder-ml-homework/" title="头歌 - 机器学习 - 随机森林"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-8.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="info text-right"><div class="info-1"><div class="info-item-1">下一篇</div><div class="info-item-2">头歌 - 机器学习 - 随机森林</div></div><div class="info-2"><div class="info-item-1">【educoder】 机器学习 --- 随机森林第1关：Bagging任务描述本关任务：补充 python 代码，完成 BaggingClassifier 类中的 fit 和 predict 函数。请不要修改 Begin-End 段之外的代码。 相关知识为了完成本关任务，你需要掌握：  什么是 Bagging； Bagging 算法如何训练与预测。  什么是Bagging Bagging 是 Bootstrap Aggregating 的英文缩写，刚接触的童鞋不要误认为 Bagging 是一种算法， Bagging 和 Boosting 都是集成学习中的学习框架，代表着不同的思想。与 Boosting 这种串行集成学习算法不同， Bagging 是并行式集成学习方法。大名鼎鼎的随机森林算法就是在 Bagging 的基础上修改的算法。 Bagging 方法的核心思想就是三个臭皮匠顶个诸葛亮。如果使用 Bagging 解决分类问题，就是将多个分类器的结果整合起来进行投票，选取票数最高的结果作为最终结果。如果使用 Bagging...</div></div></div></a></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related" href="/2023/05/04/educoder-ml-homework/" title="头歌 - 机器学习 - 决策树"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-2.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-04</div><div class="info-item-2">头歌 - 机器学习 - 决策树</div></div><div class="info-2"><div class="info-item-1">【educoder】 机器学习 --- 决策树第 1...</div></div></div></a><a class="pagination-related" href="/2023/05/09/Linear-Regression-And-Logistic-Regression/" title="线性回归和逻辑回归"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-1.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-09</div><div class="info-item-2">线性回归和逻辑回归</div></div><div class="info-2"><div class="info-item-1">线性回归和逻辑回归1. 线性回归(Linear Regression)什么是回归（Regression）回归分析是一种统计方法,于对具有一个或多个自变量的因变量(目标变量)和自变量(预测变量)之间的关系进行建模。具体地说，回归分析有助于我们理解在其他自变量保持固定的情况下，自变量的值对应于自变量的变化方式。它可以预测连续&#x2F;实际值,例如温度,年龄,工资,价格等。 通俗理解：越来越接近期望值的过程，回归于事物本来的面目。 回归是一种有监督的学习技术，有助于发现变量之间的相关性,并使我们能够基于一个或多个预测变量来预测连续输出变量。它主要用于预测，时间序列建模以及确定变量之间的因果关系。 在回归中，我们在最适合给定数据点的变量之间绘制图形,使用此图形，机器学习模型可以对数据进行预测。用简单的话说，回归显示一条线或曲线， 它穿过目标预测图上的所有数据点，以使数据点和回归线之间的垂直距离最小。数据点和线之间的距离表明模型是否已捕获牢固的关系。 主要用于预测数值型数据，典型的回归例子：数据拟合曲线 什么是线性回归（Linear...</div></div></div></a><a class="pagination-related" href="/2023/05/11/educoder-ml-homework/" title="头歌 - 机器学习 - 逻辑回归"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-5.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-11</div><div class="info-item-2">头歌 - 机器学习 - 逻辑回归</div></div><div class="info-2"><div class="info-item-1">【educoder】 机器学习 --- 逻辑回归第 1 关：逻辑回归核心思想任务描述本关任务：根据本节课所学知识完成本关所设置的编程题。 相关知识为了完成本关任务，你需要掌握：  什么是逻辑回归； sigmoid函数。  什么是逻辑回归当一看到“回归”这两个字，可能会认为逻辑回归是一种解决回归问题的算法，然而逻辑回归是通过回归的思想来解决二分类问题的算法。 那么问题来了，回归的算法怎样解决分类问题呢？其实很简单，逻辑回归是将样本特征和样本所属类别的概率联系在一起，假设现在已经训练好了一个逻辑回归的模型为 f(x) ，模型的输出是样本 x 的标签是 1 的概率，则该模型可以表示，$\widehat{p}&#x3D;f(x)$ 。若得到了样本 x 属于标签 1 的概率后，很自然的就能想到当$\widehat{p}&gt;0.5$ 时 x 属于标签 1 ，否则属于标签 0 。所以就有 $$\widehat{y} &#x3D;\begin{cases}0, &amp; \widehat{p}&lt;0.5 \[2ex]1, &amp; \widehat{p}&gt;0.5...</div></div></div></a><a class="pagination-related" href="/2023/05/26/educoder-ml-homework/" title="头歌 - 机器学习 - 随机森林"><img class="cover" src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-8.png" alt="cover"><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-26</div><div class="info-item-2">头歌 - 机器学习 - 随机森林</div></div><div class="info-2"><div class="info-item-1">【educoder】 机器学习 --- 随机森林第1关：Bagging任务描述本关任务：补充 python 代码，完成 BaggingClassifier 类中的 fit 和 predict 函数。请不要修改 Begin-End 段之外的代码。 相关知识为了完成本关任务，你需要掌握：  什么是 Bagging； Bagging 算法如何训练与预测。  什么是Bagging Bagging 是 Bootstrap Aggregating 的英文缩写，刚接触的童鞋不要误认为 Bagging 是一种算法， Bagging 和 Boosting 都是集成学习中的学习框架，代表着不同的思想。与 Boosting 这种串行集成学习算法不同， Bagging 是并行式集成学习方法。大名鼎鼎的随机森林算法就是在 Bagging 的基础上修改的算法。 Bagging 方法的核心思想就是三个臭皮匠顶个诸葛亮。如果使用 Bagging 解决分类问题，就是将多个分类器的结果整合起来进行投票，选取票数最高的结果作为最终结果。如果使用 Bagging...</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/img/avatar.png" onerror="this.onerror=null;this.src='/img/loading.gif'" alt="avatar"/></div><div class="author-info-name">David</div><div class="author-info-description">Welcome to David's Blog</div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">27</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">28</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">28</div></a></div><a id="card-info-btn" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/david-deng-01"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/david-deng-01" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://blog.csdn.net/David_0925" rel="external nofollow noreferrer" target="_blank" title="CSDN"><i class="fa fa-book-open"></i></a><a class="social-icon" href="tencent://AddContact/?fromId=45&amp;fromSubId=1&amp;subcmd=all&amp;uin=635647792&amp;website=www.oicqzone.com" rel="external nofollow noreferrer" target="_blank" title="QQ"><i class="fab fa-qq"></i></a><a class="social-icon" href="mailto:david-deng-0925@qq.com" rel="external nofollow noreferrer" target="_blank" title="Email"><i class="fas fa-envelope-open-text"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E3%80%90educoder%E3%80%91-%E6%9C%BA%E5%99%A8%E5%AD%A6%E4%B9%A0-Adaboost"><span class="toc-number">1.</span> <span class="toc-text">【educoder】 机器学习 --- Adaboost</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC1%E5%85%B3%EF%BC%9ABoosting"><span class="toc-number">1.1.</span> <span class="toc-text">第1关：Boosting</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%BB%E5%8A%A1%E6%8F%8F%E8%BF%B0"><span class="toc-number">1.1.1.</span> <span class="toc-text">任务描述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86"><span class="toc-number">1.1.2.</span> <span class="toc-text">相关知识</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E9%9B%86%E6%88%90%E5%AD%A6%E4%B9%A0"><span class="toc-number">1.1.2.1.</span> <span class="toc-text">什么是集成学习</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Boosting"><span class="toc-number">1.1.2.2.</span> <span class="toc-text">Boosting</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E8%A6%81%E6%B1%82"><span class="toc-number">1.1.3.</span> <span class="toc-text">编程要求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E8%AF%B4%E6%98%8E"><span class="toc-number">1.1.4.</span> <span class="toc-text">测试说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E7%AD%94%E6%A1%88"><span class="toc-number">1.1.5.</span> <span class="toc-text">参考答案</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC2%E5%85%B3%EF%BC%9AAdaboost%E7%AE%97%E6%B3%95"><span class="toc-number">1.2.</span> <span class="toc-text">第2关：Adaboost算法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%BB%E5%8A%A1%E6%8F%8F%E8%BF%B0-1"><span class="toc-number">1.2.1.</span> <span class="toc-text">任务描述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86-1"><span class="toc-number">1.2.2.</span> <span class="toc-text">相关知识</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E9%9B%86%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.2.2.1.</span> <span class="toc-text">数据集介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Adaboost%E7%AE%97%E6%B3%95%E5%8E%9F%E7%90%86"><span class="toc-number">1.2.2.2.</span> <span class="toc-text">Adaboost算法原理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Adaboost%E7%AE%97%E6%B3%95%E6%B5%81%E7%A8%8B"><span class="toc-number">1.2.2.3.</span> <span class="toc-text">Adaboost算法流程</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E8%A6%81%E6%B1%82-1"><span class="toc-number">1.2.3.</span> <span class="toc-text">编程要求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E8%AF%B4%E6%98%8E-1"><span class="toc-number">1.2.4.</span> <span class="toc-text">测试说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E7%AD%94%E6%A1%88-1"><span class="toc-number">1.2.5.</span> <span class="toc-text">参考答案</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC3%E5%85%B3%EF%BC%9Asklearn%E4%B8%AD%E7%9A%84Adaboost"><span class="toc-number">1.3.</span> <span class="toc-text">第3关：sklearn中的Adaboost</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%BB%E5%8A%A1%E6%8F%8F%E8%BF%B0-2"><span class="toc-number">1.3.1.</span> <span class="toc-text">任务描述</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86-2"><span class="toc-number">1.3.2.</span> <span class="toc-text">相关知识</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E9%9B%86%E4%BB%8B%E7%BB%8D-1"><span class="toc-number">1.3.2.1.</span> <span class="toc-text">数据集介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#AdaBoostClassifier"><span class="toc-number">1.3.2.2.</span> <span class="toc-text">AdaBoostClassifier</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BC%96%E7%A8%8B%E8%A6%81%E6%B1%82-2"><span class="toc-number">1.3.3.</span> <span class="toc-text">编程要求</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E8%AF%B4%E6%98%8E-2"><span class="toc-number">1.3.4.</span> <span class="toc-text">测试说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E7%AD%94%E6%A1%88-2"><span class="toc-number">1.3.5.</span> <span class="toc-text">参考答案</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2025/01/05/other-%E9%9A%8F%E7%AC%94-icarus-%E4%B8%BB%E9%A2%98%E5%AE%89%E8%A3%85/" title="Hexo 配置 Icarus 主题"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-4.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Hexo 配置 Icarus 主题"/></a><div class="content"><a class="title" href="/2025/01/05/other-%E9%9A%8F%E7%AC%94-icarus-%E4%B8%BB%E9%A2%98%E5%AE%89%E8%A3%85/" title="Hexo 配置 Icarus 主题">Hexo 配置 Icarus 主题</a><time datetime="2025-01-05T08:25:00.000Z" title="发表于 2025-01-05 16:25:00">2025-01-05</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/03/18/other-leetcode-%E3%80%902024-03-18-LeetCode-%E5%88%B7%E9%A2%98-400%E9%81%93%E6%89%93%E5%8D%A1%E3%80%91/" title="LeetCode刷题400道打卡"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="LeetCode刷题400道打卡"/></a><div class="content"><a class="title" href="/2024/03/18/other-leetcode-%E3%80%902024-03-18-LeetCode-%E5%88%B7%E9%A2%98-400%E9%81%93%E6%89%93%E5%8D%A1%E3%80%91/" title="LeetCode刷题400道打卡">LeetCode刷题400道打卡</a><time datetime="2024-03-17T16:00:00.000Z" title="发表于 2024-03-18 00:00:00">2024-03-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/01/04/2024-01-05%20%E5%A4%9A%E6%A8%A1%E6%80%81%E6%A8%A1%E5%9E%8B%E7%BB%BC%E8%BF%B0/" title="多模态基础模型：从专家到通用助理"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-7.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多模态基础模型：从专家到通用助理"/></a><div class="content"><a class="title" href="/2024/01/04/2024-01-05%20%E5%A4%9A%E6%A8%A1%E6%80%81%E6%A8%A1%E5%9E%8B%E7%BB%BC%E8%BF%B0/" title="多模态基础模型：从专家到通用助理">多模态基础模型：从专家到通用助理</a><time datetime="2024-01-04T14:42:08.000Z" title="发表于 2024-01-04 22:42:08">2024-01-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/12/30/%E6%A0%A1%E5%9B%AD%E7%BD%91%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95%E8%84%9A%E6%9C%AC/" title="校园网自动登录脚本"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-9.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="校园网自动登录脚本"/></a><div class="content"><a class="title" href="/2023/12/30/%E6%A0%A1%E5%9B%AD%E7%BD%91%E8%87%AA%E5%8A%A8%E7%99%BB%E5%BD%95%E8%84%9A%E6%9C%AC/" title="校园网自动登录脚本">校园网自动登录脚本</a><time datetime="2023-12-30T09:21:30.000Z" title="发表于 2023-12-30 17:21:30">2023-12-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/11/24/%E5%A4%9A%E6%A8%A1%E6%80%81%E8%AE%BD%E5%88%BA%E8%AF%86%E5%88%AB%E5%9F%BA%E7%BA%BF%E6%A8%A1%E5%9E%8B%E5%A4%8D%E7%8E%B0/" title="多模态讽刺识别基线模型复现"><img src="https://jsd.012700.xyz/gh/jerryc127/CDN/img/material-10.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="多模态讽刺识别基线模型复现"/></a><div class="content"><a class="title" href="/2023/11/24/%E5%A4%9A%E6%A8%A1%E6%80%81%E8%AE%BD%E5%88%BA%E8%AF%86%E5%88%AB%E5%9F%BA%E7%BA%BF%E6%A8%A1%E5%9E%8B%E5%A4%8D%E7%8E%B0/" title="多模态讽刺识别基线模型复现">多模态讽刺识别基线模型复现</a><time datetime="2023-11-24T08:51:18.000Z" title="发表于 2023-11-24 16:51:18">2023-11-24</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2025 By David</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="http://www.beian.gov.cn/portal/registerSystemInfo" rel="external nofollow noreferrer" target="_blank"> <img style="vertical-align:middle; width:20px; " src="https://cdn.jsdelivr.net/gh/David-deng-01/images/blog/icp.png"> 赣公网安备36082302000135号</a> <a href="https://beian.miit.gov.cn/" rel="external nofollow noreferrer" id="beian"  target="_blank">赣ICP备2023013705号-1</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/dist/canvas-nest.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>